<?php
namespace app\index\controller;
use \think\Controller;
use \think\Db;
use app\index\model\VerCode;
use \think\Loader;
class Login extends Controller
{
    public function index()
    {

    	$ques = (new VerCode)->getRandOne();

        return $this->fetch('',['ques'=>$ques]);
    }

    public function login(){
    	$data=input('post.');
    	$validate = Loader::validate('User');
    	$data['password']=md5($data['password']);
        // if(!$validate->scene('login')->check($data)){
        //     return ['error'=>1,'info'=>$validate->getError(),'session'=>session('answer')];
        // }
        unset($data['vercode']);
        $res = Db::table('user')->where($data)->find();

        if($res){
			session('uid',$res['uid']);
			session('username',$res['username']);
			session('avatar',$res['avatar']);

			return ['error'=>0,'info'=>'登录成功'];
		}else{
			return ['error'=>1,'info'=>'邮箱或者密码错误'];
		}
    }

    public function loginOut(){
    	session(null);
    	$this->success('退出成功', 'index/index/index');
    }

}


?>